package com.xfb.user.controller;

import com.xfb.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Auther: Xing Fang Bin
 * @Date: 2021/10/22/11:45
 * @Description:
 */
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {

    @Resource
    private UserService userService;

/*    @Autowired
    private EmailService emailService;*/

    @GetMapping("/register/{email}/{password}/{code}")
    public int register(@PathVariable String email, @PathVariable String password, @PathVariable String code) {
        return userService.selectRegisterCode(email, code);
    }

    @GetMapping("/isRegistered/{email}")
    public boolean isRegistered(@PathVariable String email) {
        System.out.println(123);
        return true;
    }

    @GetMapping("/login/{email}/{password}")
    public String login(@PathVariable String email, @PathVariable String password, HttpServletResponse response) {

        if ("xing936270749@163.com".equals(email) && "123".equals(password)) {
            String token = UUID.randomUUID().toString();
            userService.insertToken(email, token);
            Cookie cookie = new Cookie("token", token);
            cookie.setPath("/");
            response.addCookie(cookie);
            return "xing936270749@163.com";
        }
        return "false";
    }

    @GetMapping("/info/{token}")
    public String info(@PathVariable String token) {
        Map userToken = userService.selectUserToken(token);
        if (userToken != null) {
            return "xing936270749@163.com";
        }
        return "false";
    }


/*    @GetMapping("/email/{email}")
    public boolean validate(@PathVariable String email) {
        return emailService.sendEmail(email);
    }*/
}
